import { isArray } from "lodash-es"

// 处理全字段搜索的搜索
export const configToSearch = (colunm) => {
	let copyColumn = JSON.parse(JSON.stringify(colunm))
	// 先做删除处理
	copyColumn = copyColumn.filter((el, index) => {
		return (
			el.prop != 'crZoningCode' &&
			el.custome != 'divider' &&
			el.type != 'dynamic' &&
			el.type != 'dynamicForm' &&
			!(el.type == 'DividerHtml' && (copyColumn[index + 1].type == 'dynamic' || copyColumn[index + 1].type == 'dynamicForm'))
		)
	})
	if (copyColumn) {
		copyColumn.forEach((el, index) => {
			el.span = 24
			el.labelPosition = 'top'
			el.isZonghe = true

			if (el.append) {
				el.inNum = true
			}
			if (el.isNum || el.append) {
				el.label = el.label + '区间'
				el.prop = el.prop + 'Between'
				el.type = 'RangeValue'
			}
			if (el.type == 'input' || el.type == 'text') {
				el.rules = []
			}
			if (el.type == 'upload') {
				el.rules = []
				delete el.showFileList
				delete el.limit
				el.type = 'select'
				el.prop = el.prop
				el.label = '是否存在' + (el.uploadTip || el.label)
				el.dicData = [
					{ label: '是', value: '1' },
					{ label: '否', value: '0' },
				]
				el.multiple = true
			}
			if (el.type == 'remoteSelect' || el.type == 'redio' || el.type == 'select') {
				el.rules = []
				el.multiple = true
				el.prop = el.prop + 'List'
				delete el.propName
			}
			if (el.dicData) {
				el.rules = []
				el.dicData.forEach((el) => {
					if (el.value) {
						el.value = String(el.value)
					}
				})
			}
			if (el.type === 'AplidAreaSelect') {
				el.rules = []
				el.multiple = true
				// el.prop = 'crAreaId'
				el.regionGrade = 3
				el.checkStrictly = false
				el.showAllLevels = false
				delete el.propName
				if (el.label == '户籍区划') {
					el.prop = 'crAreaIdList'
				}
				if (el.label == '所属区域') {
					el.prop = 'areaId'
				}
				if (el.label == '当前所在单位所在区域') {
					el.prop = 'unitAreaIdList'
				}
				if (el.label == '所在地区' || el.label == '机构所属区域' || el.label == '所在地区(运营地)') {
					el.prop = 'nowAreaIdList'
				}
				if (el.label == '注册地区') {
					el.prop = 'registerAreaIdList'
				}
				if (el.label == '来源单位区划') {
					el.prop = 'fromAreaIdList'
				}
				if (
					el.label == '服务商所在地区' ||
					el.label == '单位所属区域' ||
					el.label == '志愿者现居地区' ||
					el.label == '机构所在地区' ||
					el.label == '所属地区' ||
					el.label == '现居地区' || 
					el.label == '所属地区(注册地)'
				) {
					el.prop = 'areaIdList'
				}
				if (el.label == '志愿者户籍所属区域' || el.label == '户籍地区') {
					el.prop = 'crAreaIdList'
				}
				if (el.label == '注册服务点所属区域') {
					el.prop = 'registerOrgAreaIdList'
				}
			}
			if (el.type == 'textarea' || el.type == 'text') {
				el.rules = []
				if (Number(el.maxlength) > 140) {
					el.maxlength = '140'
				}

				el.type = 'text'
			}

			if (el.type == 'date' || el.type == 'datetime' || el.type == 'month') {
				el.rules = []
				el.label = el.label + '区间'
				el.prop = el.prop + 'Between'
				el.type = el.type + 'range'
			}
			if (el.propOther) {
				delete el.propOther
			}
			// if(el.custome == 'divider'){
			//     // 去除分割线
			//     copyColumn.splice(index,1)
			// }
			// 如果是一对多
			// if(el.type == 'dynamic' || el.type ==  'dynamicForm'){
			//     copyColumn.splice(index,1)
			// }

			// if(el.type == 'dynamic' || el.type ==  'dynamicForm'){
			//   el.label = copyColumn[index-1].label
			//   if(el.column){
			//     let res = configToSearch(el.column)
			//     res.forEach((item,ind)=>{

			//         copyColumn.splice(index+ind+1,1,item)
			//     })
			//     copyColumn.splice(index,1)
			//   }
			//   copyColumn.splice(index,1)
			// }
		})
	}
	return copyColumn
}
// 处理全字段查缺补漏
export const configToReserveSearch = (colunm) => {
	let copyColumn = JSON.parse(JSON.stringify(colunm))
	copyColumn = copyColumn.filter((el, index) => {
		return (
			el.prop != 'crZoningCode' &&
			el.custome != 'divider' &&
			el.type != 'dynamic' &&
			el.type != 'dynamicForm' &&
			!(el.type == 'DividerHtml' && (copyColumn[index + 1].type == 'dynamic' || copyColumn[index + 1].type == 'dynamicForm'))
		)
	})
	if (copyColumn) {
		copyColumn.forEach((el, index) => {
			el.rules = []
			el.labelPosition = 'left'
			el.isChaQue = true
			// if(el.custome == 'divider'){
			//     // 去除分割线
			//     copyColumn.splice(index,1)
			// }
			// if(el.type == 'dynamic' || el.type ==  'dynamicForm'){
			//     if(el.column){
			//         let res = configToReserveSearch(el.column)
			//         res.forEach((item,ind)=>{
			//             copyColumn.splice(index+ind+1,1,item)
			//         })
			//         copyColumn.splice(index,1)
			//       }
			// }else
			if (el.propOther) {
				delete el.propOther
			}
			if (el.type === 'AplidAreaSelect') {
				if (el.label == '户籍区划') {
					el.prop = 'crAreaId1'
				}
				if (el.label == '所属区域') {
					el.prop = 'areaId1'
				}
				if (el.label == '当前所在单位所在区域') {
					el.prop = 'unitAreaId1'
				}
				if (el.label == '所在地区' || el.label == '机构所属区域') {
					el.prop = 'nowAreaId1'
				}
				if (el.label == '注册地区') {
					el.prop = 'registerAreaId1'
				}
				if (
					el.label == '服务商所在地区' ||
					el.label == '单位所属区域' ||
					el.label == '志愿者现居地区' ||
					el.label == '机构所在地区' ||
					el.label == '所属地区' ||
					el.label == '现居地区'
				) {
					el.prop = 'areaId1'
				}
				if (el.label == '志愿者户籍所属区域' || el.label == '户籍地区') {
					el.prop = 'crAreaId1'
				}
				if (el.label == '注册服务点所属区域') {
					el.prop = 'registerOrgAreaId1'
				}
				el.type = 'checkbox'
				el.span = 12
				el.dicData = [{ label: el.label || el.uploadTip, value: 1 }]
				delete el.label
				delete el.uploadTip
			} else if (el.type != 'DividerHtml') {
				el.type = 'checkbox'
				el.span = 12
				el.prop = el.prop + '1'
				el.dicData = [{ label: el.label || el.uploadTip, value: 1 }]
				delete el.label
				delete el.uploadTip
			} else {
				el.span = 24
			}
		})
	}
	return copyColumn
}
// 处理全字段搜索的请求条件
export const toSearchHandle = (config, reserveConfig, query) => {
	let arr1 = Object.keys(query)
	arr1.forEach((el) => {
		if (query[el] == '' || (Array.isArray(query[el]) && query[el].length == 0) || !query[el]) {
			query[el] = undefined
		}
	})
	// 全字段搜索
	if (config.column) {
		let list = config.column.filter((el) => {
			return (
				(el.type == 'select' || el.type == 'remoteSelect' || el.type == 'AplidAreaSelect' || el.type == 'daterange' || el.type == 'monthrange' || el.type == 'datetimerange') &&
				el.isZonghe &&
				query[el.prop]
			)
		})
		if (list.length > 0) {
			list.forEach((el) => {
				if (el.type == 'remoteSelect' || el.type == 'select') {
					if (query[el.prop] == '' && !query[el.prop]) {
						query[el.prop] = []
					}
					if (el.type == 'remoteSelect' && !Array.isArray(query[el.prop])) {
						query[el.prop] = query[el.prop].split(',')
					}
				} else if (el.type == 'AplidAreaSelect') {
					let copy = JSON.parse(JSON.stringify(query[el.prop]))
					let arr = []
					copy.forEach((areaList, ind) => {
						arr.push(areaList[2])
					})
					query[el.prop] = arr.filter(Boolean).length > 0 ? arr : query[el.prop]
				} else if (el.type == 'daterange' || el.type == 'datetimerange' || el.type == 'monthrange') {
					let str = query[el.prop].join(',')
					if (el.valueFormat == 'YYYY-MM-DD' || el.valueFormat == 'YYYY-MM') {
						str = `${query[el.prop][0]},${query[el.prop][1]}`
					}
					query[el.prop] = str
				}
			})
		}
	}
	if (reserveConfig.column) {
		let reserveList = reserveConfig.column.filter((el) => {
			return el.isChaQue && query[el.prop]
		})
		if (reserveList.length > 0) {
			reserveList.forEach((el) => {
				if (query[el.prop].length > 0) {
					query[el.prop] = 1
				} else {
					query[el.prop] = undefined
				}
			})
		}
	}
	return query
}
